<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">员工姓名：</div>
      <el-input clearable placeholder="请输入内容" class="search-main" />
      <el-button type="primary">查询</el-button>
    </div>
    <div class="create-container">
      <el-button
        type="primary"
        @click="
          dialogVisible = true
          dialogTitle = '添加员工'
        "
        >添加员工</el-button
      >
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="employeeList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="员工姓名" width="120" prop="name" />
        <el-table-column label="登录账号" width="120" prop="userName" />
        <el-table-column label="联系方式" width="180" prop="phonenumber" />
        <el-table-column label="角色" width="150" prop="roleName" />
        <el-table-column label="状态" width="100">
          <template #default="scope">
            {{ scope.row.status ? '启用' : '未启用' }}
          </template>
        </el-table-column>
        <el-table-column label="添加时间" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="150">
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              @click="editEmployee(scope.row.id)"
              >编辑</el-button
            >
            <el-button size="mini" type="text" @click="deleteUser(scope.row.id)"
              >删除</el-button
            >
            <el-button size="mini" type="text">重置密码</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        background
        layout="total, prev, pager, next"
        :total="total"
        :current-page="reqData.page"
        :page-size="reqData.pageSize"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <!-- 添加员工 -->
    <add-employee
      ref="addEmployee"
      :dialogVisible.sync="dialogVisible"
      :dialogTitle="dialogTitle"
    ></add-employee>
  </div>
</template>

<script>
import { getEmployeeListAPI } from '@/api/employee'
import AddEmployee from './conpoments/AddEmployee.vue'
import { delEmployeeAPI } from '@/api/employee'
export default {
  name: 'Employee',
  components: {
    AddEmployee
  },
  data () {
    return {
      employeeList: [],
      total: 0,
      reqData: {
        name: '',
        page: 1,
        pageSize: 2
      },
      dialogVisible: false,
      employeeId: '',
      dialogTitle: ''
    }
  },
  methods: {
    async editEmployee (id) {
      this.dialogVisible = true
      this.employeeId = id
      this.dialogTitle = '编辑员工'

      await this.$refs.addEmployee.getEmployeeById(id)
    },
    deleteUser (id) {
      this.$confirm('确认要删除该用户吗', '提示')
        .then(async () => {
          await delEmployeeAPI(id)
          if (this.reqData.page > 1 && this.employeeList.length === 1) {
            this.reqData.page--
          }
          this.$message.success('删除用户成功')
          this.getEmployeeList()
        })
        .catch(() => {})
    },
    handleCurrentChange (val) {
      this.reqData.page = val
      this.getEmployeeList()
    },

    async getEmployeeList () {
      const { data } = await getEmployeeListAPI(this.reqData)
      // console.log(data)
      this.employeeList = data.rows
      this.total = data.total
    }
  },
  created () {
    this.getEmployeeList()
  }
}
</script>

<style lang="scss" scoped>
.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container {
  margin: 10px 0px;
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.form-container {
  padding: 0px 80px;
}
</style>
